﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Admin.Management.AttendanceManage
{
    public partial class AttendanceList : SecurityPage
    {
        public override string PrivCode()
        {
            return "320f4bfbbb35c21a";
        }

        public string GetRemun(string strRemun)
        {
            string str = "";
            try { str = UtilityUI.Decode(strRemun); }
            catch { }
            return str;
        }

        private void TVBangding()
        {
            this.TV_Student.Nodes.Clear();
            List<xy_bmxbm> list = xy_bmxbm.All().ToList();
            List<DataAssociate> assXBAndBJ = DataAssociate.Where("TypeId=@0", Convert.ToInt32(EnumData.AssociateType.系别与班级)).ToList();
            List<xy_bj> bjlist = xy_bj.All().ToList();
            list.Where(p => p.BMFQID == "03").ToList().ForEach(o =>
            {
                //学院
                TreeNode xynode = new TreeNode();
                xynode.Text = o.BMMC;
                xynode.Value = o.BMDM;
                xynode.Target = Convert.ToInt32(EnumData.TvNodeType.学院).ToString();
                list.Where(k => k.BMFQID == o.BMDM && k.BMMC.Contains("系")).ToList().ForEach(j =>
                {
                    //系别
                    TreeNode xbnode = new TreeNode();
                    xbnode.Text = j.BMMC;
                    xbnode.Value = j.BMDM;
                    xbnode.Target = Convert.ToInt32(EnumData.TvNodeType.系).ToString();

                    var s_XBAndBJ = from B in assXBAndBJ where B.ParentNo == j.BMDM select B.ChildNo;
                    var q_BJ = from A in bjlist
                               where s_XBAndBJ.Contains(A.BJDM)
                               select A;
                    List<xy_bj> childbjlist = q_BJ.ToList();
                    childbjlist.ForEach(h =>
                    {
                        //班级
                        TreeNode bjnode = new TreeNode();
                        bjnode.Text = h.BJMC;
                        bjnode.Value = h.BJDM;
                        bjnode.Target = Convert.ToInt32(EnumData.TvNodeType.班级).ToString();
                        xbnode.ChildNodes.Add(bjnode);
                    });
                    xynode.ChildNodes.Add(xbnode);
                });
                this.TV_Student.Nodes.Add(xynode);
            });
            this.TV_Student.CollapseAll();
        }

        private void RepBanding()
        {
            if (this.TV_Student.SelectedNode == null) return;

            var s = from A in this.TV_Student.SelectedNode.AllNodes()
                    where A.Target == Convert.ToInt32(EnumData.TvNodeType.班级).ToString()
                    select A.Value;
            var q = from B in LinqUtility.LinqContext.xy_xsxx
                    where s.Contains(B.BJDM)
                    select B;

            int RowCount = q.Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<xy_xsxx> list = q.Skip(SkipCount).Take(PageSize).ToList();
            this.Rep_DataList.DataSource = list;
            this.Rep_DataList.DataBind();

            this.Rep_Work.DataSource = null;
            this.Rep_Work.DataBind();
        }

        private void RepWorkBanding()
        {
            string XH = this.HidSelectXH.Value;
            UserList umd = UserList.Where("XH=@0 and IsStudent=@1",XH,true).FirstOrDefault();
            if (umd == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('该用户不存在，请联系管理员！'); });</script>");
                return;
            }

            int RowCount = WorkRecord.Where("Userid=@0 and IsDel=@1", umd.id, false).Count();
            this.AspNetPager2.RecordCount = RowCount;
            int PageSize = this.AspNetPager2.PageSize;
            int SkipCount = PageSize * (this.AspNetPager2.CurrentPageIndex - 1);

            List<WorkRecord> list = WorkRecord.Where("Userid=@0 and IsDel=@1", umd.id, false).OrderByDescending(p => p.AddTime).Skip(SkipCount).Take(PageSize).ToList();
            this.Rep_Work.DataSource = list;
            this.Rep_Work.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            RepBanding();
            TVBangding();
        }

        protected void Rep_DataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string XH = Convert.ToString(e.CommandArgument);
            if (e.CommandName == "View")
            {
                this.HidSelectXH.Value = XH;
                UserList umd = UserList.Where("XH=@0 and IsStudent=@1", XH, true).FirstOrDefault();
                if (umd != null)
                    this.Lab_SelectName.Text = umd.UName;

                RepWorkBanding();
            }
        }

        protected void Rep_Work_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Edit")
            {
                this.HidIsEdit.Value = "1";
                this.HidEditId.Value = id.ToString();
                WorkRecord md = WorkRecord.Where("id=@0", id).First();
                this.txt_ProjectTitle.Text = md.ProjectTitle;
                try
                {
                    this.txt_Remuneration.Text = UtilityUI.Decode(md.Remuneration);
                }
                catch 
                {
                    this.txt_Remuneration.Text ="";
                }
                this.Drop_Years.SelectedValue = md.Years.ToString();
                this.Drop_Month.SelectedValue = md.Month.ToString();
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { Showdlg(); });</script>");
            }
            else
            {
                UserList Umd = UserList.Where("UserName=@0", User.Identity.Name).First();
                WorkRecord md = WorkRecord.Where("id=@0", id).First();
                md.IsDel = true;
                md.AddTime = DateTime.Now;
                md.AddUid = Umd.id;
                md.AddUName = Umd.UName;
                WorkRecord.Update(md, "id=@0", md.id);
                RepWorkBanding();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepBanding();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            this.RepWorkBanding();
        }

        protected void TV_Student_SelectedNodeChanged(object sender, EventArgs e)
        {
            RepBanding();
            this.Rep_Work.DataSource = null;
            this.Rep_Work.DataBind();
            this.Lab_SelectName.Text = "";
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            if (this.HidIsEdit.Value == "1")
            {
                int id = Convert.ToInt32(this.HidEditId.Value);
                UserList Umd = UserList.Where("UserName=@0", User.Identity.Name).First();
                WorkRecord md = WorkRecord.Where("id=@0", id).First();
                md.ProjectTitle = this.txt_ProjectTitle.Text.Trim();
                md.Remuneration = UtilityUI.Encode(this.txt_Remuneration.Text.Trim());
                md.Years = Convert.ToInt32(this.Drop_Years.SelectedValue);
                md.Month = Convert.ToInt32(this.Drop_Month.SelectedValue);
                md.AddUid = Umd.id;
                md.AddUName = Umd.UName;
                md.AddTime = DateTime.Now;
                WorkRecord.Update(md, "id=@0", md.id);
                RepWorkBanding();
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('修改成功！'); });</script>");
            }
            else
            {
                UserList Umd = UserList.Where("UserName=@0", User.Identity.Name).First();
                string XH = this.HidSelectXH.Value;
                UserList user = UserList.Where("XH=@0 and IsStudent=@1", XH, true).FirstOrDefault();
                WorkRecord md = new WorkRecord();
                md.ProjectTitle = this.txt_ProjectTitle.Text.Trim();
                md.Remuneration = UtilityUI.Encode(this.txt_Remuneration.Text.Trim());
                md.Years = Convert.ToInt32(this.Drop_Years.SelectedValue);
                md.Month = Convert.ToInt32(this.Drop_Month.SelectedValue);
                md.Userid = user.id;
                md.UserName = user.UName;
                md.AddUid = Umd.id;
                md.AddUName = Umd.UName;
                md.AddTime = DateTime.Now;
                WorkRecord.Insert(md);
                RepWorkBanding();
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('添加成功！'); });</script>");
            }
        }
    }
}